var localEnvForm = new Vue({
	el: '#localEnvForm',
	data: {
		area:[],
		des:'',
		photos: [], //图片集合
		uploadcount: 0, //判断结束
		dbid:''

	},
	
	
	watch: {　　　　　
		uploadcount:function(curVal, oldVal) {
			
			var _this = this
			
			var l =  this.photos.length 
			if(curVal != 0 && curVal == l) {
				setTimeout(function() {
					plus.nativeUI.closeWaiting();

					mui.alert("提交成功",function(){})
				}, 2000);

			}
		}　　
	},
	mounted: function() {
		var id = this.geturlparam("id")
		this.localdata(id)
		this.dbid= id
	},
	methods: {
		localdata:function(id){
			
			var _this = this;

			//从本地数据库取数据
			db.transaction(function (tx) {
			    tx.executeSql('SELECT * FROM area WHERE id = ?', [Number(id)], function (tx, results) {
							
			 		_this.des = results.rows.item(0).des
			 		document.getElementById('showCityPicker3').value = results.rows.item(0).areaname
			 		_this.area = results.rows.item(0).area.split(',')
			 	
			 		
			 		var len = results.rows.length, i;

       				for (i = 0; i < len; i++){
 						
       					//var newUrlAfterCompress = _this.compressImage(results.rows.item(i).localurl, results.rows.item(i).path);
       					_this.photos.push({
							"localurl": results.rows.item(i).localurl,
							"path": results.rows.item(i).path,
							"uploaded": false
						});

       				}
			 		
   				}, null); 
			});
		},
		geturlparam: function(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r != null) return decodeURI(r[2]);
			return null;
		},
		savelocal:function(){
			//表单信息验证
			if(this.area.length<=0){
				mui.alert("请选择行政区域")
				return
			}
			if(this.des.length<=0){
				mui.alert("请填写区域介绍")
				return
			}
			if(this.photos.length<=0){
				mui.alert("请添加产地环境图片")
				return
			}
			
			//图片本地缓存
			var _this = this
			db.transaction(function (tx) {  
						tx.executeSql('CREATE TABLE IF NOT EXISTS area (id,area,areaname,des,localurl,path)');
						tx.executeSql('SELECT * FROM area', [], function (tx, results) {
					      	 var len = results.rows.length;
					      	 if(len==null){len=0}
							//循环遍历图片数组存储
							for(var i=0;i<_this.photos.length;i++){
								tx.executeSql('INSERT INTO area (id,area,areaname,des,localurl,path) VALUES (?,?,?,?,?,?)',[len+1,_this.area,document.getElementById('showCityPicker3').value,_this.des,_this.photos[i].localurl,_this.photos[i].dstname]);	
							}
							
   				}, null); 			      		
			});
			
		},
		submit:function(){
			//表单信息验证
			if(this.area.length<=0){
				mui.alert("请选择行政区域")
				return
			}
			if(this.des.length<=0){
				mui.alert("请填写区域介绍")
				return
			}
			if(this.photos.length<=0){
				mui.alert("请添加产地环境图片")
				return
			}
			plus.nativeUI.showWaiting();
			//保存表单保存图片
			
			var route = '';
			var areastr = '';
			for(var i = 0; i < this.photos.length; i++) {
				if(i==0){
					route  = this.photos[i].path
				}else{
			
					route  = route +","+ this.photos[i].path
				}
						
			}
			for(var i = 0; i < this.area.length; i++) {
				if(i==0){
					areastr  = this.area[i]
				}else{
			
					areastr  = areastr +","+ this.area[i]
				}
						
			}

			var _this = this
			axios({
				url: sever + '/saveAreaInfo',
				method: 'POST',
				// 请求体重发送的数据
				params: {
				area:areastr,des:_this.des,photo:route,
				},
				timeout: 50000,
				// 设置请求头
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				}
			}).then(function(res) {
				if(res.data.success) {
				
				//上传物照
				for(var i = 0; i < _this.photos.length; i++) {
						_this.upload(_this.photos[i].path);
				}
				
				db.transaction(function(tx) {
    					tx.executeSql('DELETE FROM area WHERE id=?', [Number(_this.dbid)]);
				});
					
				}else{
					//plus.nativeUI.closeWaiting();
					//提交失败
					mui.toast("请求出错，请稍后再试");
				}
			})
		},
			upload: function(file) {
			//判断
		
			var _this = this
			var task = plus.uploader.createUpload(sever + "/uploadEnv", {
					method: "POST",
					blocksize: 204800,
					priority: 100
				},
				function(t, status) {
					// 上传完成
					if(status == 200) {
						_this.uploadcount =_this.uploadcount + 1
					} else {
						plus.nativeUI.closeWaiting();
						mui.alert("上传文件出现问题");
					}
				}
			);
			task.addFile(file, {
				key: file
			});
	
			task.start()
			},
		//选择获取照片方式
		choosePhoto: function() {
			var bts = [{
				title: "拍照"
			}];
			var _this = this;
			plus.nativeUI.actionSheet({
					cancel: "取消",
					buttons: bts
				},
				function(e) {
					if(e.index == 1) {
						_this.getImage();
					} else if(e.index == 2) {
						_this.galleryImgs();
					}
				}
			);
		},
		getImage: function() {
			var newUrlAfterCompress = '';
			var dstname = '';
			var cmr = plus.camera.getCamera();
			var _this = this;
			cmr.captureImage(function(p) {
				plus.io.resolveLocalFileSystemURL(p, function(entry) {
					var localurl = entry.toLocalURL(); //   
					dstname = "_downloads/" + _this.getUid() + ".jpg"; //设置压缩后图片的路径   
					newUrlAfterCompress = _this.compressImage(localurl, dstname);
					_this.photos.push({
						"localurl": localurl,
						"path": dstname,
						"uploaded": false
					});
			
			
				});
				
				
			
				
				
			});
		},
		galleryImgs: function() { // 从相册中选择图片   
			var _this = this;
			plus.gallery.pick(function(e) {
				
				var had = _this.photos.length
				for(var i = 0; i < e.files.length; i++) {
					if(i + had < 8) {
						var dstname = "_downloads/" + _this.getUid() + ".jpg"; //设置压缩后图片的路径   
						_this.compressImage(e.files[i], dstname);

						_this.photos.push({
							"localurl": e.files[i],
							"path": dstname,
							"uploaded": false
						});

					}
				}
			}, function(e) {
				console.log("取消选择图片");
			}, {
				filter: "image",
				multiple: true
			});
		},
		getUid: function() {
			return Math.floor(Math.random() * 100000000 + 10000000).toString();
		},
		compressImage: function(src, dstname) {
			plus.zip.compressImage({
					src: src,
					dst: dstname,
					overwrite: true,
					quality: 20
				},
				function(event) {
				
					return event.target;
				},
				function(error) {
					console.log(error);
					return src;
				}

			);
		
		},
		delphoto: function(index) {
			this.photos.splice(index, 1);
		},

	},
});